﻿#define _CRT_SECURE_NO_WARNINGS 1

//动态规划第十二题
//12.简单多状态 dp 问题_打家劫舍II_C++
//https://leetcode.cn/problems/house-robber-ii/

class Solution {
public:
    int rob(vector<int>& nums) {
        int n = nums.size();
        return max(nums[0] + rob1(nums, 2, n - 2), rob1(nums, 1, n - 1));
    }
    int rob1(vector<int>& nums, int left, int right) {
        // 细节处理
        if (left > right)
            return 0;

        int n = nums.size();
        vector<int> f(n);
        auto g = f;
        f[left] = nums[left];

        for (int i = left + 1; i <= right; i++) {
            f[i] = g[i - 1] + nums[i];
            g[i] = max(f[i - 1], g[i - 1]);
        }

        return max(f[right], g[right]);
    }
};